package cn.java79.quiz.web;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author oldliu
 * @since 1.0
 */
public class HelloServlet extends HttpServlet {
    public HelloServlet(){
        System.out.println("Helloservlet被创建了。。。");
    }

    @Override
    public void init() throws ServletException {
        System.out.println("Helloservlet被初始化。。。");
        String name = super.getServletConfig().getInitParameter("name");
        System.out.println(name);
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //System.out.println("helloservlet 等待被客户端请求"+req.getMethod());
        super.service(req, resp);
//        String r=req.getMethod();
//        if(r.equals("GET")){
//            doGet(req, resp);
//        }else if(r.equals("POST")){
//            doPost(req, resp);
//        }
    }

    @Override
    public void destroy() {
        System.out.println("helloServlet被销毁了");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //必须在getWriter之前
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();//out对应
        writer.write("<!DOCTYPE html>");
        writer.write("<html>");
        writer.write("<body>");
        writer.write("<h2>这是你的get请求</h2>");
        writer.write("<h2>"+req.getClass().getName()+"</h2>");
        writer.write("<h2>"+resp.getClass().getName()+"</h2>");
        writer.write("</body>");
        writer.write("</html>");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();//out对应
        writer.write("<!DOCTYPE html>");
        writer.write("<html>");
        writer.write("<body>");
        writer.write("<h2>这是你的post请求</h2>");
        writer.write("<h2>"+req.getClass().getName()+"</h2>");
        writer.write("</body>");
        writer.write("</html>");
    }


}
